package com.example.fundappcation.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresApi;

/**
 * 判断网络状态
 */
public class NetWorkUtils {

//    public static int getNetWorkType(Context applicationContext) {
//        int result = 0;
//        ConnectivityManager connectivityManager = (ConnectivityManager) applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE);
//
//        Network networkInfo = null;
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//            networkInfo = connectivityManager.getActiveNetwork();
//        }
//
//        if (networkInfo != null && networkInfo.isConnected()) {
//            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {//是否是wifi
//                result = 2;
//            } else if (networkInfo.getType() == connectivityManager.TYPE_MOBILE) {//是否是移动网络
//                switch (networkInfo.getSubtype()) {
//                    case TelephonyManager.NETWORK_TYPE_GPRS://2G
//                    case TelephonyManager.NETWORK_TYPE_EDGE:
//                    case TelephonyManager.NETWORK_TYPE_CDMA:
//                    case TelephonyManager.NETWORK_TYPE_1xRTT:
//                    case TelephonyManager.NETWORK_TYPE_IDEN:
//                        result = 3;
//                        break;
//                    case TelephonyManager.NETWORK_TYPE_UMTS:
//
//                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
//                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
//                    case TelephonyManager.NETWORK_TYPE_HSDPA:
//                    case TelephonyManager.NETWORK_TYPE_HSUPA:
//                    case TelephonyManager.NETWORK_TYPE_EVDO_B:
//
//                    case TelephonyManager.NETWORK_TYPE_EHRPD:
//                    case TelephonyManager.NETWORK_TYPE_HSPAP://3G
//                        result = 4;
//                        break;
//                    case TelephonyManager.NETWORK_TYPE_LTE:
//                        result = 5;
//                        break;
//
//
//                }
//
//            }
//        }
//        return result;
//    }

    /**
     * api为 23以下的手机使用这个判断网络状态
     *
     * @param context
     */
    public void checkState_21(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //步骤2：获取ConnectivityManager对象对应的NetworkInfo对象
        //NetworkInfo对象包含网络连接的所有信息
        //步骤3：根据需要取出网络连接信息
        //获取WIFI连接的信息
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        Boolean isMobileConn = networkInfo.isConnected();
        //获取移动数据信息
        networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        Boolean isMobileCon = networkInfo.isConnected();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void checkState_23(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        Network[] networks = connectivityManager.getAllNetworks();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < networks.length; i++) {
            NetworkInfo networkInfo = connectivityManager.getNetworkInfo(networks[i]);
            stringBuffer.append(networkInfo.getTypeName() + "connct is " + networkInfo.isConnected());
        }
    }
}
